package com.ruoyi.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.pojo.Car;
import com.ruoyi.pojo.CarEntyApproval;
import com.ruoyi.pojo.vo.CarVo;
import com.ruoyi.service.CarEntyApprovalService;
import com.ruoyi.service.CarService;
import com.ruoyi.utils.CarExamine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @program: ry-cloud
 * @author: mxf
 * @description:
 * @create: 2024-10-18 11:36
 */
@RestController
@RequestMapping("/driver/car")
public class DriverCarController {

    @Autowired
    private CarEntyApprovalService carEntyApprovalService;

    @Autowired
    private CarService carService;

    //查询没有审核的车辆信息
    @GetMapping("noExamineList")
    public List<Car> noExamineList(){
        return carService.carList();
    }

    //根据单个id查询数据
    @GetMapping("/getById/{id}")
    public Car getById(@PathVariable Long id){
        return carService.getById(id);
    }

    //审核日志
    @Transactional
    @PostMapping("/IdUpeStatus")
    public boolean carIdUpeStatus(@RequestBody CarVo carVo){
        //获取数据
        String causeOfRejection = carVo.getCauseOfRejection();
        //根据车辆id获取数据
        Car car = carService.getById(carVo.getId());
        //将值放到carEntyApproval对象中
        CarEntyApproval carEntyApproval = BeanUtil.toBean(car, CarEntyApproval.class);
        //判读有没有驳回原因
        if (Objects.isNull(causeOfRejection)){
            //审核通过

            car.setState(CarExamine.CAR_STATE_YES);
            carEntyApproval.setState(CarExamine.CAR_STATE_YES);
            carService.upe(car);
            //添加记录
            carEntyApprovalService.add(carEntyApproval);
            return true;
        }
        //添加驳回原因
        carEntyApproval.setCauseOfRejection(carVo.getCauseOfRejection());
        //审核失败
        car.setState(CarExamine.CAR_STATE_NO);
        carEntyApproval.setState(CarExamine.CAR_STATE_YES);
        carService.upe(car);
        //添加记录
        carEntyApprovalService.add(carEntyApproval);
        return false;
    }


}
